function getAlarmHis() {
    // $("#tasklist").remove()
    // var tab = document.createElement("table")
    // tab.setAttribute('id', 'tasklist');
    $.ajax({
        url: 'http://localhost:3000/alarm/getAlarmHis',
        data: {},
        success: function(data) {
            $("#alarHislist").find("tr").remove();
            ConvertToTableWithOption(data.list, "alarHislist") // tasklist is the id of html
        },
    })
}


// set default create data
function createTask() {
    // check form fill
    let title = $("#title").val()
    let detail = $("#content").val()
    let createDate = $("#createDate").val()
    if (!title) {
        $("#titleNotice").html("*Please input title")
        $("#title").focus(); //获取焦点
        return;
    } else {
        $("#titleNotice").html("")
    }
    console.log("create")
    $.ajax({
        url: 'http://localhost:3000/task/new',
        data: {
            'title': title,
            'detail': detail,
            'date': createDate
        },
        success: function(data) {
            if (data.code == 200) {
                alert('login success');
                window.location.href = "tasklist.html";
            } else {
                alert('User not exist or password incorecct...')
            }
        },
        error: function(data) {
            console.log('error');
            console.log(data)
        }
    })
}

// 创建新的任务时，时间控件默认填值为当前时间
$(document).ready(function() {
    var createDate = $("#createdate").val();

    if (!createDate) {
        let now = new Date();
        //格式化日，如果小于9，前面补0
        let day = ("0" + now.getDate()).slice(-2);
        //格式化月，如果小于9，前面补0
        let month = ("0" + (now.getMonth() + 1)).slice(-2);
        //拼装完整日期格式
        let today = now.getFullYear() + "-" + (month) + "-" + (day);
        //完成赋值
        console.log("put new date:" + today)
        $("#createdate").val(today);

    } else {
        console.log(createDate)
    }

});


function removeAlarmRecord(obj) {
    var tr = obj.parentNode.parentNode;
    tr.parentNode.removeChild(tr);
}

function exportToCSV(jsonData) {

    //列标题，逗号隔开，每一个逗号就是隔开一个单元格
    let str = `姓名,电话,邮箱\n`;
    //增加\t为了不让表格显示科学计数法或者其他格式
    for (let i = 0; i < jsonData.length; i++) {
        for (let item in jsonData[i]) {
            str += `${jsonData[i][item] + '\t'},`;
        }
        str += '\n';
    }
    //encodeURIComponent解决中文乱码
    let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);
    //通过创建a标签实现
    let link = document.createElement("a");
    link.href = uri;
    //对下载的文件命名
    link.download = "json数据表.csv";
    link.text = "download"
    document.body.appendChild(link);
    // link.click();
    // document.body.removeChild(link);
}